package com.landinfo.web.rest;

import com.codahale.metrics.annotation.Timed;
import com.landinfo.domain.SystemSetting;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;


/**
 * 地图相关
 */
@RestController
@RequestMapping("/api/map")
public class MapResource {
    private final Logger log = LoggerFactory.getLogger(MapResource.class);

    @Autowired
    private SystemSetting systemSetting;

    /**
     * GET  /data-types : get all the dataTypes.
     *
     * @return the ResponseEntity with status 200 (OK) and the list of dataTypes in body
     */
    @RequestMapping(value = "/downloadTpk", method = RequestMethod.GET)
    @Timed
    public ResponseEntity<InputStreamResource> getAllDataTypes() throws IOException {
        log.debug("REST request to get all DataTypes");
        String filePath =systemSetting.getTpkPath();    // "/Users/ziqilv/大地吉信/项目/瓦屋山/source/test10.5.tpk";

        FileSystemResource file = new FileSystemResource(filePath);
        HttpHeaders headers = new HttpHeaders();
        headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
        headers.add("Content-Disposition", String.format("attachment; filename=\"%s\"", file.getFilename()));
        headers.add("Pragma", "no-cache");
        headers.add("Expires", "0");
        return ResponseEntity
                .ok()
                .headers(headers)
                .contentLength(file.contentLength())
                .contentType(MediaType.parseMediaType("application/octet-stream"))
                .body(new InputStreamResource(file.getInputStream()));
    }
}
